﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using UtilityExtensions;

namespace BOL
{
    [Serializable]
 public static   class BuscarBL
    {
     public static List<SearchResult> GetEntidad(string termino, int count)
     {
         string[] word = new string[2];
                  
         string[] word2 = termino.Split(' ');
         word[0] = word2[0];
         word[1] = (word2.Length > 1) ? termino.Substring(word2[0].Length) : "";
         

         int idEmpleado= int.Parse(System.Web.HttpContext.Current.Session["idEmpleado"].ToString());
         

         var obr = from x in DbUtil.Db.EmpleadoObras
                   where x.idEmpleado == idEmpleado 
                   select new
                   {
                       idInternal=x.Obra.idInternal,
                       descripcion=x.Obra.descripcion,
                       proyecto=x.Obra.Proyecto.descripcion
                   };

         var habiObra = from x in DbUtil.Db.EmpleadoObras
                   where x.idEmpleado == idEmpleado 
                   select x.idObra;
         
         var q= (from a in  DbUtil.Db.view_buscars
                 join b in obr on a.idObra equals b.idInternal  into viewObra
                 from c in viewObra.DefaultIfEmpty()
                 where a.descripcion.ToLower().Contains(word[0]) &
               a.descripcion.ToLower().Contains(word[1]) &
         
                 (habiObra.Contains(a.idObra.Value) |a.idObra==null)
                 select new SearchResult
                {
                    aplicacion=a.Aplicacion.ToString(),
                    descripcion=a.descripcion,
                    idObra=a.idObra.ToString(),
                    proyecto=c.proyecto,
                    obra=c.descripcion,
                    url=a.url,
                    vista=a.vista  ,
                    
                }).Take(count);

                return q.ToList();
    
     }

     public static List<EmailResult> GetEMail(string termino, int count)
     {
         string[] word = new string[2];

         string[] word2 = termino.Split(' ');
         word[0] = word2[0];
         word[1] = (word2.Length > 1) ? termino.Substring(word2[0].Length) : "";




         var q = (from a in DbUtil.Db.view_MailSugeridos
                  where a.email.ToLower().Contains(word[0])
                  select new EmailResult
                  {

                      email = a.email,
                      descripcion = a.descripcion,
                      vista = a.vista,

                  }).Take(count);

         return q.ToList();

     }
    }
}
